# 添加redis模块引用
import redis


def search_key():
    # 创建Redis连接池
    # host：连接主机
    # port：端口号，默认6379
    # db：数据库，默认0-15
    pool = redis.ConnectionPool(host='testing.redis.union.com', port=6379, db=0)

    # 从连接池中获取一个连接
    r = redis.StrictRedis(connection_pool=pool)

    t_keys = r.keys('*-*-*-*-*')
    print('符合图形验证码条件的key总个数：', len(t_keys))

    a = 0
    b = 0
    # 循环遍历符合图形验证码的keys
    for i in range(len(t_keys)):
        key = t_keys[i]
        value = r.get(key)
        ttl = r.ttl(key)
        print(key)
        print(value)
        print(ttl)
        if a < ttl:
            a = ttl
            b = key
    print(a)
    print(r.get(b))


if __name__ == '__main__':
    search_key()